<?php
	
function get_price($idMay)
	{
		$result=mysql_query("select Gia from may where idMay='$idMay'");
		$row=mysql_fetch_array($result);
		return $row["Gia"];	
	}
function get_product_name($idMay)
	{
		$result=mysql_query("select Gia from may where idMay='$idMay'");
		$row=mysql_fetch_array($result);
		return $row["ThongTin"];	
	}

function remove_product($idMay)
	{
		$max=count($_SESSION['GioHang']);
		for($i=0;$i<$max;$i++)
		{
			if($idMay==$_SESSION['GioHang'][$i]['idMay'])
			{
				unset($_SESSION['GioHang'][$i]);
				break;
			}
		}
		$_SESSION['GioHang']=array_values($_SESSION['GioHang']);
	}
function get_order_total()
	{
		$max=count($_SESSION['GioHang']);
		$sum=0;
		for($i=0;$i<$max;$i++)
		{
			$idMay=$_SESSION['GioHang'][$i]['idMay'];
			$SoLuong=$_SESSION['GioHang'][$i]['SoLuong'];
			$Gia=get_price($idMay);
			$sum+=$Gia*$SoLuong;
		}
		return $sum;
	}
function addcart($idMay,$SoLuong,$Gia,$UrlHinh)
{
	
	if(is_array($_SESSION['GioHang'])) {
		if(product_exists($idMay)==-1)
		{
			$max=count($_SESSION['GioHang']);
			$_SESSION['GioHang'][$max]['Gia']=$Gia;
			$_SESSION['GioHang'][$max]['idMay']=$idMay;
			$_SESSION['GioHang'][$max]['SoLuong']=$SoLuong;
			$_SESSION['GioHang'][$max]['UrlHinh'] = $UrlHinh;
		}
		else
			$_SESSION['GioHang'][product_exists($idMay)]['SoLuong']+=$SoLuong;
	}
	else
	{	$_SESSION['GioHang']=array();
		$_SESSION['GioHang'][0]['Gia']=$Gia;
		$_SESSION['GioHang'][0]['idMay']=$idMay;
		$_SESSION['GioHang'][0]['SoLuong']=$SoLuong;
		$_SESSION['GioHang'][0]['UrlHinh'] = $UrlHinh;
	}
}
function product_exists($idMay)
{
	$max=count($_SESSION['GioHang']);
$flag=false;
for($i=0;$i<$max;$i++) {
if($idMay==$_SESSION['GioHang'][$i]['idMay']){
	$flag=true;
	break;
}
}
if($flag)
return $i;
return -1;
}
?>
